package com.hna.eking.Controller;

import java.util.List;

import org.mybatis.spring.MyBatisSystemException;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hna.eking.ConfigUtils.GetConfig;
import com.hna.eking.Exception.RunTimeException;
import com.hna.eking.iBatis.TableInfoImp;
import com.hna.eking.iBatis.TableInfoMapper;

@RestController
@MapperScan("com.hna.eking.iBatis")
@EnableConfigurationProperties(GetConfig.class)
public class GetTableInfo {
	@Autowired
	private TableInfoImp TableInfo;
	
	@Autowired
	private GetConfig Config;
	
	private static final Logger logger = LoggerFactory.getLogger(GetTableInfo.class);
	
	@RequestMapping(value = "/TableInfo", method=RequestMethod.OPTIONS)
	public String GetTableItemInfo() {
		logger.debug("test" + Config.getZookeeper_host());
		return "success";
	}
	@RequestMapping(value = "/TableInfo", method=RequestMethod.GET)
	public String GetTableItemInfo(@RequestParam(value="TableName") String TableName) {
		JSONObject json = new JSONObject();
		if(TableName == null || "".equals(TableName)){
			json.put("ErrorCode", RunTimeException.WEB_CONTROL_STATUS_PARM_NULL);
			return json.toJSONString();
		}
		try{
			List<TableInfoMapper> TableList =  TableInfo.getByTableName(TableName);
			if(TableList.size() != 0){
				
				json.put("ErrorCode", RunTimeException.WEB_CONTROL_STATUS_SUCC);				
				json.put("TableName", TableList.get(0).getTable_Name());
				json.put("FilePath", TableList.get(0).getFile_Path());
				json.put("TableAlias", TableList.get(0).getTable_ALIAS());
				json.put("FileSeq", TableList.get(0).getFile_Seq());
				return json.toJSONString();
			}else{
				logger.debug("data not found");
				json.put("ErrorCode", RunTimeException.WEB_DATA_NOT_FOUND);
				return json.toJSONString();
			}
			
		}catch(MyBatisSystemException e){
			logger.error("mybatis error" + e.getMessage());
			json.put("ErrorCode", RunTimeException.MAIN_SQL_ERROR_EXCEPTION);
			return json.toJSONString();
		}		
	}
	
	@RequestMapping(value = "/TableInfo", method=RequestMethod.POST)
	public String PostTableItemInfo(@RequestParam(value="TableInfo") String TableEntry) {
		logger.debug("TableInfo update begin");
		JSONObject json = JSONObject.parseObject(TableEntry);
		
		TableInfoMapper TableList = new TableInfoMapper();
		TableList.setTable_Name(json.getString("TableName"));
		TableList.setFile_Path(json.getString("FilePath"));
		TableList.setTable_ALIAS(json.getString("TableAlias"));
		TableList.setFile_Seq(json.getString("FileSeq"));
		
		int iRet = TableInfo.InsertTableInfo(TableList);
		JSONObject ret = new JSONObject();
		ret.put("ErrorCode", RunTimeException.WEB_CONTROL_CREATE_SUCC);
		return ret.toJSONString();
	}
	
	@RequestMapping(value = "/TableInfo", method=RequestMethod.DELETE )
	public String DelTableItemInfo(@RequestParam(value="TableName") String TableEntry) {
		TableInfo.DeleteTableInfo(TableEntry);
		JSONObject ret = new JSONObject();
		ret.put("ErrorCode", RunTimeException.WEB_CONTROL_DETE_SUCC);
		return ret.toJSONString();
	}
	
}